<?php
namespace Home\Server;

use \Home\Server\getRedisService;

/**
 * redis 数据方面 控制
 */
class RedisData
{
    public $redis;

    function __construct()
    {
        $this ->redis = ( new getRedisService() ) ->redis;

        if( ! $this ->redis )
        {
            return False;
        }
    }

    /**
     * Function JS_RecipientAssist
     *
     * Author: 黄盖
     * Date: 2017-3-29 15:42
     * Info: 将收件人的信息存入redis
     *          辅助JS循环操作
     */
    public function JS_RecipientAssist()
    {
        echo
'
<script src="/Public/static/js/jquery.min.js" type="text/javascript"></script>
<div>
    请稍候....<br/>
</div>
<script>
$(document).ready(function()
{
    function setRecipientData( page )
    {
        $.get("/index.php?m=Home&c=Simulate&a=entry&server_class=RedisData&server_function=setRecipientData&page=" + page, {}, function( data )
        {
            $("div").append( data.msg );
            
            if( data.code == "1" )
            {
                setRecipientData( page + 1 );
            }
        }, "json");
    }
    
    setRecipientData( 1 );
});
</script>
';
        exit;
    }

    /**
     * Function setRecipientData
     *
     * Author: 黄盖
     * Date: 2017-3-29 11:37
     * Info: 将收件人的信息存入redis
     */
    public function setRecipientData()
    {
        //操作3数据库
        $this ->setRedis_DB( 3 );

        //每次操作数量
        $limit = 10000;

        //数据量太大分批导入
        $page = intval( $_GET['page'] );
        $page = $page <= 1 ? 1 : $page;

        //起始条数
        $startNums = ( $page - 1 ) * $limit;

        //获取分页数据
        $Recipient_Data = $this ->getAllRecipient_Data( $startNums, $limit );

        //组建APP需要的数据
        $this ->Organize_Label( $Recipient_Data );

        //数据量
        $oneNums = count( $Recipient_Data );


        //输出  如果量不够, 则不再继续循环
        echo json_encode( Array( 'code' => $oneNums == $limit ? '1' : '0' , 'msg' =>  "第{$page}页: " . $startNums . " - " . ( $startNums + $oneNums ) . '导入完成!<br/>' ) );
        exit;
    }

    /**
     * Function First_RecipientData
     * @param $Uid
     * @param array $recipientData
     *
     * Author: 黄盖
     * Date: 2017-3-30 17:34
     * Info: 第一次入库
     *          记录初始化信息
     */
    public function First_RecipientData( $recipientData = Array(), $indexID )
    {
        //操作3数据库
        $this ->setRedis_DB( 3 );

        foreach( $recipientData as $key => $val )
        {
            $val['fId'] = $indexID + $key;

            /**
             * 写入redis
             *      站点 与 收件人 对应 一条数据
             */
            $this ->setData_Redis( "recipient_label_{$val['fRecipientMobile']}_{$val['fUserID']}" , $val );
        }
    }

    /**
     * Function setRecipient_OneData
     * @param $Uid
     * @param $recipient_mobile
     *
     * Author: 黄盖
     * Date: 2017-3-30 10:05
     * Info: 更新指定收件人信息
     */
    public function setRecipient_OneData( $Uid, $recipient_mobile )
    {
        //操作3数据库
        $this ->setRedis_DB( 3 );

        $RecipientData = $this ->RecipientResult() ->where("`f_user_id` = '{$Uid}' AND `f_recipient_mobile` = '{$recipient_mobile}'") ->select();

        //组建APP需要的数据
        $this ->Organize_Label( $RecipientData );
    }

    /**
     * Function Organize_Label
     * @param $Recipient_Data
     *
     * Author: 黄盖
     * Date: 2017-3-29 11:25
     * Info: 创建一个方法
     */
    public function Organize_Label( $Recipient_Data )
    {
        foreach( $Recipient_Data as $key => $val )
        {
            //将标签序列化存储
            $val['label'] = json_encode( $this ->getRecipient_Label( $val ) );

            /**
             * 写入redis
             *      站点 与 收件人 对应 一条数据
             */
            $this ->setData_Redis( "recipient_label_{$val['fRecipientMobile']}_{$val['fUserID']}" , $val );
        }
    }

    /**
     * Function getAllRecipient_Data
     * @return mixed
     *
     * Author: 黄盖
     * Date: 2017-3-27 18:12
     * Info: 收件人信息
     *          一次查出内存溢出, 分批处理
     */
    public function getAllRecipient_Data( $start = 0, $limit = 250 )
    {
        return $this ->RecipientResult() ->limit("{$start}, {$limit}") ->select();
    }

    /**
     * Function RecipientResult
     * @return \Think\Model
     *
     * Author: 黄盖
     * Date: 2017-3-27 18:12
     * Info: 首先人信息数据库资源
     */
    public function RecipientResult()
    {
        return M('tp_mobile_info')
                ->field('
                            `f_id` AS `fId`, 
                            `f_user_id` AS `fUserID`, 
                            `f_recipient_mobile` AS `fRecipientMobile`, 
                            `f_recipient_name` AS `fRecipientName`, 
                            `f_recipient_phone` AS `fRecipientPhone`, 
                            `f_address` AS `fAddress`, 
                            `f_status` AS `fStatus`
                        ')
                ;
    }

    /**
     * Function getRecipient_Label
     * @param array $Recipient_Data
     *
     * Author: 黄盖
     * Date: 2017-3-29 11:17
     * Info: 获取收件人的标签
     */
    public function getRecipient_Label( $Recipient_Data = Array() )
    {
        return M('tp_receive_label')
                    ->field('
                        `tp_receive_label`.`f_receive_id`,
                        `tp_receive_label`.`f_id`,
                        `tp_label`.`f_label`
                    ')
                    ->join('LEFT JOIN `tp_label` ON `tp_receive_label`.`f_receive_label_id` = `tp_label`.`f_id`')
                    ->where("`tp_receive_label`.`f_receive_id` = {$Recipient_Data['fId']} AND `tp_receive_label`.`f_type` = 0")
                    ->select()
                    ;

    }

    /**
     * Function setRedis_DB
     * @param int $DB
     *
     * Author: 黄盖
     * Date: 2017-3-29 11:38
     * Info: 设定操作哪个redis数据库
     */
    private function setRedis_DB( $DB = 0 )
    {
        $this ->redis ->select( $DB );
    }

    /**
     * Function setData_Redis
     * @param string $keyName
     * @param array $Data
     *
     * Author: 黄盖
     * Date: 2017-3-29 11:33
     * Info: 将数据存到redis
     */
    private function setData_Redis( $keyName = '', $Data = Array() )
    {
        //删除
        $this ->redis ->del( $keyName );

        //写入
        return $this ->redis ->HMset( $keyName, $Data );
    }
}